package com.yeswater.multithread.chapter6;

import java.io.*;

/**
 * author : yeswater
 * date : 2023/10/24
 */

public class Run_6_1_6 {
    public static void main(String[] args) {
        try {
            MyObject myObject = new MyObject();
            System.out.println("反序列化-myObject=" + myObject.hashCode() + " userinfo=" + myObject.userinfo.hashCode());
            FileOutputStream fos = new FileOutputStream("myObjectFile.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(myObject);
            oos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            FileInputStream fis = new FileInputStream(new File("myObjectFile.txt"));
            ObjectInputStream ois = new ObjectInputStream(fis);
            MyObject myObject = (MyObject) ois.readObject();
            ois.close();
            fis.close();
            System.out.println("序列化-myObject=" + myObject.hashCode() + " userinfo=" + myObject.userinfo.hashCode());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static class Userinfo {

    }

    private static class MyObject implements Serializable {

        private static final long serialVersionUID = -5611927642145445253L;
        public static Userinfo userinfo = new Userinfo();
        private static MyObject myObject = new MyObject();

        private MyObject() {

        }

        public static MyObject getInstance() {
            return myObject;
        }

        // protected Object readResolve() throws ObjectStreamException {
        //     System.out.println("invoke readResolve method");
        //     return MyObject.myObject;
        // }
    }

}
